#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/sched.h>
#include <linux/reboot.h>
#include <linux/pwm.h>
#include <linux/gpio.h>
#include <linux/hrtimer.h>
#include <linux/platform_device.h>
#include <linux/clk-provider.h>
#include <linux/clkdev.h>
#include <linux/clk.h>
#include "gpio_comsumer_driver.h"

struct virt_clk_consumer
{
	struct clk *clk;
};
static int virt_gpio_clk_consumer_probe(struct platform_device *pdev)
{
	struct virt_gpio_consumer_data *pdata = (struct virt_gpio_consumer_data *)(pdev->dev.platform_data);
	struct virt_clk_consumer *consumer_ptr = NULL;

	printk("%s %d\n", __FUNCTION__, __LINE__);
	consumer_ptr = devm_kzalloc(&pdev->dev, sizeof(struct virt_clk_consumer), GFP_KERNEL);
		
	if (!consumer_ptr)
		return -ENOMEM;

	printk("%s %d\n", __FUNCTION__, __LINE__);
	consumer_ptr->clk = clk_get(&pdev->dev, pdata->con_id);	
	if (IS_ERR(consumer_ptr->clk)) 
	{
		return PTR_ERR(consumer_ptr->clk);
	}
	printk("%s %d\n", __FUNCTION__, __LINE__);
	platform_set_drvdata(pdev, consumer_ptr);
	clk_prepare_enable(consumer_ptr->clk);
	return 0;
}

static int virt_gpio_clk_consumer_remove(struct platform_device *pdev)
{
	struct virt_clk_consumer *consumer_ptr = platform_get_drvdata(pdev);

	clk_disable_unprepare(consumer_ptr->clk);
	clk_put(consumer_ptr->clk);
	printk("%s %d\n", __FUNCTION__, __LINE__);
	return 0;
}

#ifdef CONFIG_OF
static const struct of_device_id of_gpio_clk_consumer_match[] = {
	{ .compatible = "virt_gpio_consumer", },
	{},
};
#endif
static struct platform_driver gpio_clk_gate_driver = {
	.probe		= virt_gpio_clk_consumer_probe,
	.remove		= virt_gpio_clk_consumer_remove,
        .driver         = {
                .name   = "virt_gpio_consumer",
                .owner  = THIS_MODULE,
                .of_match_table = of_match_ptr(of_gpio_clk_consumer_match),
        },
};

module_platform_driver(gpio_clk_gate_driver);

MODULE_AUTHOR("jerry_chg");
MODULE_DESCRIPTION("gpio clk gate driver");
MODULE_LICENSE("GPL");

